package com.yfapp.platform.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yfapp.platform.dao.template.DbUtilsTemplate;
import com.yfapp.platform.entity.Admin;
import com.yfapp.platform.service.AdminService;
import com.yfapp.platform.tools.Page;
import com.yfapp.platform.tools.Pagination;

@Controller
@RequestMapping("/admin")
public class AdminController {
	@Autowired
	private AdminService adminService;
	@Autowired
	protected DbUtilsTemplate dbUtilsTemplate;

	/**
	 * 管理员登陆
	 * @param admin    登陆传入的用户对象
	 * @return String  要跳转到的页面
	 */
	@RequestMapping(value = "/login")
	public String login(HttpServletRequest request, HttpServletResponse response, Admin admin) throws Throwable {
		Admin ad =new Admin();
		if(null!=admin.getAdminPwd()||null!=admin.getAdminCode()){
			ad = adminService.login(admin);
		}else{
			ad=null;
		}
		if (null != ad) {
			request.getSession().setAttribute("admin", ad);
			return "/indexhome";
		} else {
			request.setAttribute("imgs", "请重新登录！");
			return "/login";
		}
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/adminInfolist",method = RequestMethod.POST)
	@ResponseBody
	public <T> Object infolist1(Page page,HttpServletRequest request, HttpServletResponse response,String id) throws Throwable {
		List<Admin> adminList = adminService.getAdminList();
		Pagination<T> listBean = dbUtilsTemplate.listBean((List<T>)adminList, page);
		return listBean;
	}
}
